package com.tdx.blog_nowcoder.utils.event;

import com.alibaba.fastjson.JSONObject;
import com.tdx.blog_nowcoder.entity.MyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.HashMap;
import java.util.Hashtable;

/**
* @description: TODO
* @author 何可人
* @date 2022/7/5 21:57
* @version 1.0
*/

@Component
public class EventProducer {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    //发布事件
    public void fireEvent(MyEvent event){
        // 将事件发布到指定的主题


        ListenableFuture<SendResult<String, Object>> future =  kafkaTemplate.send(event.getTopic(), JSONObject.toJSONString(event));
//        future.addCallback(result -> logger.info("生产者成功发送消息到topic:{} partition:{}的消息", result.getRecordMetadata().topic(), result.getRecordMetadata().partition()),
//                ex -> logger.error("生产者发送消失败，原因：{}", ex.getMessage()));
    }
}
